﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using WebCore.Tools;

namespace WebCore.Dtos
{
    public enum FieldTypeCode
    {
        Auto = 0,
        Text = 1,
        Date = 2,
        Enumable = 3,
        Dic = 4,
        Number = 5,
        DateTime = 6,
        MultipleText = 7,
        KeyWord = 8,
    }
    public class Condition
    {
        public string FieldLabel { get; set; }
        public object FieldValue { get; set; }
        public string FieldName { get; set; }
        public FieldTypeCode FieldType { get; set; }
        public DateTime? FieldValueStart { get; set; }
        public DateTime? FieldValueEnd { get; set; }
    }
    public class HashObject:Dictionary<string,object>
    {
        public object this[string key, bool returnNullIfNotExistsKey = true]
        {
            get
            {
                bool flag = returnNullIfNotExistsKey && !base.ContainsKey(key);
                object result;
                if (flag)
                {
                    result = null;
                }
                else
                {
                    result = this[key];
                }
                return result;
            }
            set
            {
                this[key] = value;
            }
        }
    }
    public class HashObjectList : List<HashObject> { 
    
    }
    /// <summary>
    /// 分页查询对象
    /// </summary>
    public class PagedQueryCondition
    {
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public bool ColDefsOnly { get; set; }
        public HashObject Params { get; set; }
        public HashObject OrderBy { get; set; }

        public (string , HashObject) BuildSqlParam<T>()
        {
            StringBuilder where = new StringBuilder();
            HashObject result = new HashObject();
            if (Params == null || Params.Count <= 0) return (null, result);
            int index = 0;
            var initdate = DateTime.MinValue;
            var type = typeof(T);
            
            foreach (var item in Params)
            {
                try
                {
                    var condition = GetCondition(item.Value);
                    if (condition == null) continue;
                    
                    object pararmValue = null;
                    switch (condition.FieldType)
                    {
                        case FieldTypeCode.Text:
                            buildLike(condition);
                            break;
                        case FieldTypeCode.Date:
                        case FieldTypeCode.DateTime:
                            buildDateRange(condition);
                            break;
                        case FieldTypeCode.Dic:
                        case FieldTypeCode.Enumable:
                            buildIn(condition);
                            break;
                        case FieldTypeCode.Number:
                        case FieldTypeCode.KeyWord:
                        default:
                            buildEquals(condition);
                            break;
                    }
                   
                }
                catch
                {
                    continue;
                }

            }
            return (where.ToString(), result);
            void buildEquals(Condition condition) {
                var pararmkey = getPararmkey();
                where.Append($" AND {condition.FieldName}=@{pararmkey} ");
                Params.Add(pararmkey, $"{condition.FieldValue.ToString().Trim() }");
            }
            void buildIn(Condition condition) {
                var pararmkey = getPararmkey();
                if (string.IsNullOrEmpty(condition.FieldValue + "")) return;
                var val = condition.FieldValue.ToString().ToJsonObject<List<dynamic>>();//枚举参数
                if (val == null || val.Count <= 0) return;
                where.Append($" AND {condition.FieldName} IN @{pararmkey} ");
                Params.Add(pararmkey, val);
            }
            void buildDateRange(Condition condition) {
                if (condition.FieldValueStart.HasValue && condition.FieldValueStart.Value > initdate)
                {
                    var pararmkey = getPararmkey();
                    where.Append($" AND {condition.FieldName}>=@{pararmkey} ");
                    Params.Add(pararmkey, condition.FieldType == FieldTypeCode.Date ? condition.FieldValueStart.Value.Date : condition.FieldValueStart.Value);
                }
                if (condition.FieldValueEnd.HasValue && condition.FieldValueEnd.Value > initdate)
                {
                    var pararmkey = getPararmkey();
                    where.Append($" AND {condition.FieldName}<=@{pararmkey} ");
                    Params.Add(pararmkey, condition.FieldType == FieldTypeCode.Date ? condition.FieldValueEnd.Value.Date : condition.FieldValueEnd.Value);
                }
            }
            void buildLike(Condition condition) {
                var pararmkey = getPararmkey();
                where.Append($" AND {condition.FieldName} LIKE @{pararmkey} ");
                result.Add(pararmkey, $"{condition.FieldValue.ToString().Trim() }%");
            }
            string getPararmkey() {
                return $"param{++index}";
            }
            Condition GetCondition(object obj) {
                if (string.IsNullOrEmpty(obj + "")) return null;
                var condition = obj.ToJsonString().ToJsonObject<Condition>();
                if (condition == null || string.IsNullOrEmpty(condition.FieldName)) return null;
                if (string.IsNullOrWhiteSpace(condition.FieldValue + "")
                    && string.IsNullOrWhiteSpace(condition.FieldValueEnd + "")
                    && string.IsNullOrWhiteSpace(condition.FieldValueStart + ""))
                    return null;
                // 验证是否是类属性 todo


                return condition;
            }
        }

    }
    public class PagedResponseBase<T>: DataResponseBase<List<T>>
    { 
        public long TotalNum { get; set; }
    }
    public class DataResponseBase<T>
    {
        public T Data { get; set; }
    }
    public class ResponseBase
    {
        public ResponseBase()
        {
            this.IsSuccess = true;
        }
        public ResponseBase(string errorMsg, bool isSuccess = false)
        {
            this.IsSuccess = isSuccess;
            this.OperationDesc = errorMsg;
        }
        /// <summary>
        ///     操作结果
        /// </summary>
        public bool IsSuccess { get; set; }
        public bool IsFailed { get => !IsSuccess; }
        /// <summary>
        /// 操作描述
        /// </summary>
        public string OperationDesc { get; set; }
        /// <summary>
        ///     失败的话显示可以自动消失的提示，而不是错误弹框
        /// </summary>
        public bool ShowFailedNotification { get; set; } = false;
    }
}
